__all__ = ['Regions']
from ..storage import *
class Regions(Storage):

    SQL_DROP = ["""
        DROP TABLE IF EXISTS "regions"
    """]

    SQL_CREATE = ["""
        CREATE TABLE "regions" (
            "region_id"             integer         not null,
            "name"                  text            not null,
            PRIMARY KEY ("region_id")
        ) WITHOUT OIDS
    """]

    SQL_INSERT = ["""
        INSERT INTO "regions" (
            "region_id",
            "name"
        ) VALUES (
            %(region_id)s,
            %(name)s
        )
    """]

    SQL_ANALYZE = ["""
        ANALYZE "regions"
    """]

    def drop(self):
        self.execute(self.SQL_DROP)

    def create(self):
        self.execute(self.SQL_CREATE)

    def insert(self, rows):
        self.execute(self.SQL_INSERT, rows);

    def analyze(self):
        self.execute(self.SQL_ANALYZE)

    def adapt(self, fetcher):
        return\
            map(lambda row: {
                'region_id'     : row['regionID'        ],
                'name'          : row['regionName'      ],
            },
            filter(lambda row: row['regionName'] != '',
            fetcher({
                'regionID'      : int,
                'regionName'    : str,
            }, 'mapRegions')))
